<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="标题" prop="title"
                :rules="[
                  {required: true, message:'标题不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写标题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="类型" prop="type"
                :rules="[
                  {required: true, message:'类型不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.type" placeholder="请填写类型"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="升级包路径" prop="path"
                :rules="[
                  {required: true, message:'升级包路径不能为空', trigger:'blur'}
                 ]">
              <el-upload ref="path"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/smis/version/jfVersion`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.path = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.path = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="pathArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="最新编码" prop="newestCode"
                :rules="[
                  {required: true, message:'最新编码不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.newestCode" placeholder="请填写最新编码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="最新版本号" prop="newestVersion"
                :rules="[
                  {required: true, message:'最新版本号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.newestVersion" placeholder="请填写最新版本号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="最低编码" prop="lowestCode"
                :rules="[
                  {required: true, message:'最低编码不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.lowestCode" placeholder="请填写最低编码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="最低版本号" prop="lowestVersion"
                :rules="[
                  {required: true, message:'最低版本号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.lowestVersion" placeholder="请填写最低版本号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="更新内容" prop="content"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.content" placeholder="请填写更新内容"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        pathArra: [],
        inputForm: {
          id: '',
          title: '',
          type: '',
          path: '',
          newestCode: '',
          newestVersion: '',
          lowestCode: '',
          lowestVersion: '',
          content: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建版本升级`
        } else if (method === 'edit') {
          this.title = '修改版本升级'
        } else if (method === 'view') {
          this.title = '查看版本升级'
        }
        this.pathArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/version/jfVersion/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfVersion)
              this.inputForm.path.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.pathArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/version/jfVersion/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>